<template>
  <el-dialog :title="!dataForm.userId ? '新增' : '修改'"
             :close-on-click-modal="false"
             :visible.sync="visible">
    <el-form :model="dataForm"
             :rules="dataRule"
             ref="dataForm"
             @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <el-form-item label="用户名"
                    prop="username">
        <el-input v-model="dataForm.username"
                  placeholder="用户名"></el-input>
      </el-form-item>
      <el-form-item label="手机号"
                    prop="tel">
        <el-input v-model="dataForm.tel"
                  placeholder="手机号"></el-input>
      </el-form-item>
      <el-form-item label="密码"
                    prop="password">
        <el-input v-model="dataForm.password"
                  placeholder="密码"></el-input>
      </el-form-item>
      <el-form-item label="余额"
                    prop="money">
        <el-input v-model="dataForm.money"
                  placeholder="余额"></el-input>
      </el-form-item>
      <el-form-item label="权限"
                    prop="flag">
        <el-radio-group v-model="dataForm.flag">
          <el-radio :label="0">用户</el-radio>
          <el-radio :label="1">经纪人</el-radio>
        </el-radio-group>
      </el-form-item>
      <span v-if="dataForm.userImg!=''">
        <img :src="dataForm.userImg"
             width="80px"
             height="80px">
      </span>
      <el-form-item label="头像"
                    prop="userImg">
        <el-upload class="upload-demo"
                   action="http://localhost:8888/api/user/houseuser/upload"
                   :file-list="fileList"
                   list-type="picture"
                   :on-success="onSuccess">
          <el-button size="small"
                     type="primary">点击上传</el-button>
          <div slot="tip"
               class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
        </el-upload>
      </el-form-item>
    </el-form>
    <span slot="footer"
          class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary"
                 @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data () {
    return {
      fileList: [],
      visible: false,
      dataForm: {
        userId: 0,
        username: '',
        tel: '',
        password: '',
        createTime: '',
        money: '',
        flag: '',
        userImg: ''
      },
      dataRule: {
        username: [
          { required: true, message: '用户名不能为空', trigger: 'blur' }
        ],
        tel: [
          { required: true, message: '手机号不能为空', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' }
        ],
        createTime: [
          { required: true, message: '创建时间不能为空', trigger: 'blur' }
        ],
        money: [
          { required: true, message: '余额不能为空', trigger: 'blur' }
        ],
        flag: [
          { required: true, message: '表示(0.用户 1.经纪人)-》可选不能为空', trigger: 'blur' }
        ],
        userImg: [
          { required: true, message: '头像不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    'visible' (val) {
      if (val) {
        this.fileList = []
      }
    }
  },
  methods: {
    onSuccess (response, file, fileList) {
      console.log(response, file)
      this.dataForm.userImg = response.url
    },
    init (id) {
      this.dataForm.userId = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.userId) {
          this.$http({
            url: this.$http.adornUrl(`/user/houseuser/info/${this.dataForm.userId}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.username = data.houseUser.username
              this.dataForm.tel = data.houseUser.tel
              this.dataForm.password = data.houseUser.password
              this.dataForm.createTime = data.houseUser.createTime
              this.dataForm.money = data.houseUser.money
              this.dataForm.flag = data.houseUser.flag
              this.dataForm.userImg = data.houseUser.userImg
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/user/houseuser/${!this.dataForm.userId ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'userId': this.dataForm.userId || undefined,
              'username': this.dataForm.username,
              'tel': this.dataForm.tel,
              'password': this.dataForm.password,
              'createTime': this.dataForm.createTime,
              'money': this.dataForm.money,
              'flag': this.dataForm.flag,
              'userImg': this.dataForm.userImg
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
